﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using Model;
using System.Threading;
using System.Net;
using Common;
using System.Diagnostics;
using Database;

namespace GameHelp
{
    public delegate void OperationMessageWrite(Account account, string message);//用于在主程序界面中显示信息的代理
    public delegate bool OperationWaitResult(byte[] datas, int length, OperationWaitResultPoint point);//用于当发起一个操作后等待验证服务器返回结果的代理
    public delegate void OperationWaitResultTimeout(int timeout);//用于当监听器超时的委托
    /// <summary>
    /// 当游戏角色已进入服务器，可以关闭客户端的操作委托
    /// </summary>
    public delegate void WhenConnected(Account account);

    /// <summary>
    /// 操作执行后验证结果
    /// </summary>
    public class OperationWaitResultPoint
    {
        bool sucess = false;
        bool stoped = false;//监听器已停止
        bool enabled = true;//监听器是否启用
        string message = "";
        OperationWaitResult method;
        int timeout = 0;
        DateTime startTime;
        ManualResetEvent resetEvent = new ManualResetEvent(false);

        /// <summary>
        /// 验证的超时时间，单位秒，指定为0则永不超时
        /// </summary>
        public int Timeout
        {
            get { return timeout; }
            set { timeout = value; }
        }

        /// <summary>
        /// 返回的信息
        /// </summary>
        public string Message
        {
            get { return message; }
            set { message = value; }
        }

        /// <summary>
        /// 执行操作结果验证的方法
        /// </summary>
        public OperationWaitResult Method
        {
            get { return method; }
            set { method = value; }
        }

        /// <summary>
        /// 返回是否已经超时
        /// </summary>
        public bool IsTimeout
        {
            get
            {
                if (timeout == 0) { return false; }
                TimeSpan timeSpan = DateTime.Now - this.startTime;
                if (timeSpan.Milliseconds > timeout * 1000)
                {
                    if (this.OnTimeout != null)
                    {
                        Thread thread = new Thread(new ParameterizedThreadStart(delegate(object o) { OnTimeout(timeout); }));
                        thread.Start();
                    }
                    resetEvent.Set();
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// 操作执行是否成功
        /// </summary>
        public bool Sucess
        {
            get { return sucess; }
            set
            {
                if (value)
                {
                    resetEvent.Set();
                }
                sucess = value;
            }
        }

        /// <summary>
        /// 监听器是否已停止
        /// </summary>
        public bool Stoped
        {
            get { return stoped; }
            set
            {
                if (value)
                {
                    resetEvent.Set();
                }
                stoped = value;
            }
        }

        /// <summary>
        /// 监听器是否启用
        /// </summary>
        public bool Enabled
        {
            get { return enabled; }
            set
            {
                if (value)
                {
                    resetEvent.Reset();
                
                }
                else
                {
                    resetEvent.Set();
                }
                enabled = value;
            }
        }

        /// <summary>
        /// 等待监听器执行完毕或者超时时间已到
        /// </summary>
        /// <returns></returns>
        public bool Wait()
        {
            if (timeout > 0)
            {
                if (!resetEvent.WaitOne(timeout * 1000))
                {
                    if (this.OnTimeout != null)
                    {
                        OnTimeout(this.timeout);
                    }
                    resetEvent.Set();
                    return false;
                }
                resetEvent.Set();
                return true;
            }
            else
            {
                return resetEvent.WaitOne();
            }
        }

        public OperationWaitResultPoint()
        {
            resetEvent.Reset();
        }
        /// <summary>
        /// 构造一个OperationWaitResultPoint
        /// </summary>
        /// <param name="timeout">指定超时时间，单位秒，指定为0则永不超时</param>
        public OperationWaitResultPoint(int timeout)
        {
            this.timeout = timeout;
            startTime = DateTime.Now;
            resetEvent.Reset();
        }

        /// <summary>
        /// 当监听器超时并且检测了监听器是否超时时引发
        /// </summary>
        public event OperationWaitResultTimeout OnTimeout;
    }

    /// <summary>
    /// 操作类
    /// </summary>
    public partial class Operationer
    {
        #region private data
        private TcpClient serverClient;
        private Account account;
        private List<OperationWaitResultPoint> waitResultMethod;
        OperationWaitResultPoint getExpListener = null;
        bool stopCurrent = false;//停止当前流程进行下一个流程
        bool stopAll = false;//停止所有流程
        bool paused = false;
        object pauseLocker = new object();//用于暂停的线程锁
        ManualResetEvent pauseEvent = new ManualResetEvent(true);
        Thread thread = null;
        Thread expTaskThread = null;
        Timer expFlushTimer = null;
        ExpTask currentExpTask = null;//当前循环经验任务

        #endregion

        /// <summary>
        /// 获取当前自动游戏是否已暂停
        /// </summary>
        public bool Paused
        {
            get { return paused; }
        }

        /// <summary>
        /// 停止当前自动游戏流程进行下一个流程
        /// </summary>
        public void StopCurrent()
        {
            this.stopCurrent = true;
        }

        /// <summary>
        /// 停止当前自动游戏线程，并将所有任务都设置为已完成
        /// </summary>
        public void StopAll()
        {
            stopAll = true;
        }

        /// <summary>
        /// 停止当前自动游戏线程，并将所有任务都设置为已完成
        /// </summary>
        public void StopAll(bool setQuestCompleted)
        {
            stopAll = true;

            GameState state = Manager.GetState(account);
            if (state != null)
            {
                state.DailyQuestCompleted = true;
                state.Radio30Completed = true;
                state.Radio50Completed = true;
                state.Radio70Completed = true;
                state.Radio80Completed = true;
                state.ExpQuestCompleted = true;
            }
        }

        /// <summary>
        /// 重新开始所有任务
        /// </summary>
        public void RestartAll()
        {
            GameState state = Manager.GetState(account);
            if (state != null)
            {
                state.DailyQuestCompleted = false;
                state.Radio30Completed = false;
                state.Radio50Completed = false;
                state.Radio70Completed = false;
                state.Radio80Completed = false;
                state.ExpQuestCompleted = false;
            }
            stopAll = false;
            BeginQuest();
        }
        /// <summary>
        /// 暂停自动游戏
        /// </summary>
        public void Pause()
        {
            paused = true;
            pauseEvent.Reset();
        }

        /// <summary>
        /// 继续自动游戏
        /// </summary>
        public void Continue()
        {
            paused = false;
            pauseEvent.Set();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="account">账户</param>
        /// <param name="serverClient">服务端连接</param>
        /// <param name="writeMethod">信息输出方法</param>
        public Operationer(Account account, TcpClient serverClient, OperationMessageWrite writeMethod)
        {
            try
            {
                this.serverClient = serverClient;
                this.account = account;
                MessageWrite = writeMethod;
                waitResultMethod = new List<OperationWaitResultPoint>(15);

                //注册获取角色信息的监听器
                OperationWaitResultPoint getCharactersExtInfo = new OperationWaitResultPoint();
                getCharactersExtInfo.Method = new OperationWaitResult(GetCharactersExtInfo);
                getCharactersExtInfo.Timeout = 300;
                getCharactersExtInfo.OnTimeout += new OperationWaitResultTimeout(getCharactersExtInfo_OnTimeout);
                waitResultMethod.Add(getCharactersExtInfo);

                //注册获取角色等级的监听器
                OperationWaitResultPoint getCharactersLevel = new OperationWaitResultPoint();
                getCharactersLevel.Method = new OperationWaitResult(GetCharactersLevelInfo);
                getCharactersLevel.Timeout = 300;
                getCharactersLevel.OnTimeout += new OperationWaitResultTimeout(getCharactersLevel_OnTimeout);
                waitResultMethod.Add(getCharactersLevel);

                //注册获取经验值的监听器
                getExpListener = new OperationWaitResultPoint();
                getExpListener.Method = new OperationWaitResult(GetExp);
                waitResultMethod.Add(getExpListener);
            }
            catch (Exception ex)
            {
                Tools.PrintException(ex);
            }
        }

        /// <summary>
        /// 当游戏角色已进入服务器，可以关闭客户端的操作委托
        /// </summary>
        public event WhenConnected OnConnected;

        /// <summary>
        /// 将数据转发到指定的连接，一般用于不期望响应的操作，比如某角色说话的信息
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="length"></param>
        /// <param name="client"></param>
        /// <returns>返回是否转发成功</returns>
        public bool Forward(byte[] datas, int length, TcpClient client)
        {
            try
            {
                client.GetStream().Write(datas, 0, length);
            }
            catch (Exception ex)
            {
                //调试信息记录
                Tools.PrintException(ex);
                return false;
            }
            return true;
        }

        /// <summary>
        /// 将数据发送到服务端
        /// </summary>
        /// <param name="datas"></param>
        private void Forward(byte[] datas)
        {
            Forward(datas, datas.Length, serverClient);
        }

        public OperationMessageWrite MessageWrite;


        /// <summary>
        /// 处理数据
        /// </summary>
        public void ProcessData(byte[] datas, int length)
        {
            try
            {
                List<OperationWaitResultPoint> list = new List<OperationWaitResultPoint>();

                foreach (OperationWaitResultPoint point in this.waitResultMethod)
                {
                    if (point.Stoped || point.IsTimeout)//已超时或者已完成使命则移除
                    {
                        list.Add(point);
                    }
                    else if (point.Enabled)
                    {
                        point.Method(datas, length, point);
                    }
                }

                //移出
                foreach (OperationWaitResultPoint point in list)
                {
                    waitResultMethod.Remove(point);
                }
            }
            catch (Exception ex)
            {
                Tools.PrintException(ex);
            }
        }

        /// <summary>
        /// 在客户端已关闭的情况下保持服务端连接
        /// </summary>
        public void KeepUpOnline()
        {
            Thread thread = new Thread(new ParameterizedThreadStart(delegate(object o)
            {
                while (serverClient.Connected)
                {
                    Forward(SysCheckOnline);
                    Thread.Sleep(60000);//一分钟发送一次客户端在线代码
                }
            }));
            thread.Start();
        }

        #region private method

        /// <summary>
        /// 开始任务
        /// </summary>
        private void BeginQuest()
        {
            //开始按照配置文件进行自动游戏
            thread = new Thread(new ParameterizedThreadStart(autoGame));
            thread.Start();
        }

        /// <summary>
        /// 自动游戏线程方法
        /// </summary>
        /// <param name="characterName"></param>
        private void autoGame(object charObject)
        {
            GameConfig config = Manager.GetConfig(account);
            if (config == null)//不存在配置文件
            {
                return;
            }
            Thread.Sleep(10000);//休息5秒钟等待客户端和服务端的数据通讯完毕

            try
            {
                fluashExpTaskClolor(config);
                autoExpQuest(config);
            }
            catch (Exception ex)
            {
                SysWriteMessage("进行老板娘任务时出现了意外的错误，任务可能没有完成。");
                Tools.PrintException(ex);
            }

            try
            {
                SysWriteMessage("开始老板娘任务。");
                autoDailyQuest(config);//老板娘任务
                SysWriteMessage("已完成老板娘任务。");
            }
            catch (Exception ex)
            {
                SysWriteMessage("进行老板娘任务时出现了意外的错误，任务可能没有完成。");
                Tools.PrintException(ex);
            }

            try
            {
                SysWriteMessage("开始自动挂机。");
                if (config.AutoGuaji) { AutoGuji(); }//自动挂机
                SysWriteMessage("已经自动挂机。");
            }
            catch (Exception ex)
            {
                SysWriteMessage("进行挂机时出现了意外的错误，挂机可能没有进行。");
                Tools.PrintException(ex);
            }

            try
            {
                SysWriteMessage("开始进行30副本。");
                autoRadio30(config);//30副本
                SysWriteMessage("已完成30副本。");
            }
            catch (Exception ex)
            {
                SysWriteMessage("进行30副本时出现了意外的错误，副本可能没有完成。");
                Tools.PrintException(ex);
            }
            try
            {
                SysWriteMessage("开始进行50副本。");
                autoRadio50(config);//50副本 
                SysWriteMessage("已完成50副本。");
            }
            catch (Exception ex)
            {
                SysWriteMessage("进行50副本时出现了意外的错误，副本可能没有完成。");
                Tools.PrintException(ex);

            }
            try
            {
                SysWriteMessage("开始进行70副本。");
                autoRadio70(config);//70副本
                SysWriteMessage("已完成70副本。");
            }
            catch (Exception ex)
            {
                SysWriteMessage("进行70副本时出现了意外的错误，副本可能没有完成。");
                Tools.PrintException(ex);
            }
            try
            {
                SysWriteMessage("开始进行80副本。");
                autoRadio80(config);//80副本
                SysWriteMessage("已完成80副本。");
            }
            catch (Exception ex)
            {
                SysWriteMessage("进行80副本时出现了意外的错误，副本可能没有完成。");
                Tools.PrintException(ex);
            }

            thread = null;
        }

        #region 30困难副本

        /// <summary>
        /// 接受30困难副本任务
        /// </summary>
        /// <returns></returns>
        public bool QuestacceptRadio30()
        {
            MessageWrite(account, "接受30困难副本任务...");
            Forward(QuestacceptRadio30ACode);
            Thread.Sleep(1000);
            Forward(QuestacceptRadio30BCode);
            Thread.Sleep(1000);
            Forward(QuestacceptRadio30CCode);
            return true;
        }

        /// <summary>
        /// 提交30困难副本任务
        /// </summary>
        /// <returns></returns>
        public bool FinshQuestRadio30()
        {
            MessageWrite(account, "提交30困难副本任务...");
            Forward(FinshQuestRadio30ACode);
            Thread.Sleep(1000);
            Forward(FinshQuestRadio30BCode);
            Thread.Sleep(1000);
            Forward(FinshQuestRadio30CCode);
            return true;
        }

        /// <summary>
        /// 进入30副本
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool EnterRadio30(RadioType type)
        {
            if (type == RadioType.Hard)
            {
                Forward(Operationer.RadioHard30EnterCode);
            }
            else
            {
                Forward(Operationer.RadioEasy30EnterCode);
            }
            return true;
        }

        /// <summary>
        /// 30副本进入下一层战斗
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool FightRadio30(RadioType type)
        {
            if (type == RadioType.Hard)
            {
                Forward(Operationer.RadioHard30NextFightCode);
            }
            else
            {
                Forward(Operationer.RadioEasy30NextFightCode);
            }
            return true;
        }

        /// <summary>
        /// 30副本自动游戏引擎
        /// </summary>
        /// <param name="config"></param>
        private void autoRadio30(GameConfig config)
        {
            if (!config.AutoRadio30)
            {
                MessageWrite(account, "配置文件设置为：不自动进行30副本，已跳过。");
                return;
            }

            if (stopAll)
            {
                MessageWrite(account, "已手动停止所有任务。");
                return;
            }

            GameState state = Manager.GetState(account);
            if (state.Radio30Date.ToShortDateString().Equals(DateTime.Now.ToShortDateString()))
            {
                if (state.Radio30Completed)
                {
                    MessageWrite(account, "今天的30副本已完成。");
                    return;
                }
            }

            if (config.RadioQuestType == RadioQuest.Radio30)
            {
                QuestacceptRadio30();
            }

            if (config.Radio30Type == RadioType.Hard)
            {
                EnterRadio30(RadioType.Hard);
                MessageWrite(account, "进入30困难副本.");
            }
            else
            {
                EnterRadio30(RadioType.Easy);
                MessageWrite(account, "进入30简单副本.");
            }

            state.Radio30Date = DateTime.Now;
            int count = 1;
            bool radioComplete = false;
            OperationWaitResultPoint checkCompleteRadioPoint = new OperationWaitResultPoint();
            checkCompleteRadioPoint.Method = delegate(byte[] data, int length, OperationWaitResultPoint point)
            {
                radioComplete = FightRadio30Sucess(data, length);//检测是否通关
                point.Sucess = radioComplete;
                point.Stoped = radioComplete;
                return radioComplete;//如果已通关会从检测结果方法队列中删除
            };
            this.waitResultMethod.Add(checkCompleteRadioPoint);


            OperationWaitResultPoint getNextTimePoint = new OperationWaitResultPoint();
            int time = 0;
            getNextTimePoint.Method = delegate(byte[] data, int length, OperationWaitResultPoint point)
            {
                time = GetNextFightTime(data, length);
                return true;
            };
            this.waitResultMethod.Add(getNextTimePoint);

            while (!radioComplete)
            {
                if (stopAll)
                {
                    MessageWrite(account, "已手动停止所有任务，退出30副本任务。");
                    getNextTimePoint.Stoped = true;//停止监听器
                    checkCompleteRadioPoint.Stoped = true;
                    return;
                }

                if (this.stopCurrent)
                {
                    MessageWrite(account, "已停止自动30副本.");
                    getNextTimePoint.Stoped = true;//停止监听器
                    checkCompleteRadioPoint.Stoped = true;
                    state.Radio30Completed = true;
                    this.stopCurrent = false;
                    return;
                }
                pauseEvent.WaitOne();//暂停
                MessageWrite(account, "进入30" + EnumConverter.GetString(config.Radio30Type) + "副本第" + count + "层战斗");
                FightRadio30(RadioType.Hard);

                WaitCommandSucess(getNextTimePoint, 10);//等待操作执行结果
                if (time > 0)
                {
                    MessageWrite(account, "还需要等待" + time + "秒才能进行下次战斗.");
                    Thread.Sleep(time * 1000);
                    continue;
                }

                Thread.Sleep(3000 + count * 1000);//战斗时间
                FinshFight();//结束战斗


                MessageWrite(account, "第" + (count++) + "层战斗结束");
                //WaitCommandSucess(point, 10);
            }
            state.Radio30Completed = true;
            getNextTimePoint.Stoped = true;//停止监听器
            checkCompleteRadioPoint.Stoped = true;
            if (config.RadioQuestType == RadioQuest.Radio30)
            {
                FinshQuestRadio30();//提交副本任务
            }

            MessageWrite(account, "30副本已通关.");
        }
        #endregion

        #region 50困难副本

        /// <summary>
        /// 接受50困难副本任务
        /// </summary>
        /// <returns></returns>
        private bool QuestacceptRadio50()
        {
            MessageWrite(account, "接受50困难副本任务...");
            Forward(QuestacceptRadio50ACode);
            Thread.Sleep(1000);
            Forward(QuestacceptRadio50BCode);
            Thread.Sleep(1000);
            Forward(QuestacceptRadio50CCode);
            return true;
        }

        /// <summary>
        /// 提交50困难副本任务
        /// </summary>
        /// <returns></returns>
        public bool FinshQuestRadio50()
        {
            MessageWrite(account, "提交50困难副本任务...");
            Forward(FinshQuestRadio50ACode);
            Thread.Sleep(1000);
            Forward(FinshQuestRadio50BCode);
            Thread.Sleep(1000);
            Forward(FinshQuestRadio50CCode);
            return true;
        }

        /// <summary>
        /// 50副本进入下一层战斗
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool FightRadio50(RadioType type)
        {
            if (type == RadioType.Hard)
            {
                Forward(Operationer.RadioHard50NextFightCode);
            }
            else
            {
                Forward(Operationer.RadioEasy50NextFightCode);
            }
            return true;
        }

        /// <summary>
        /// 自动50副本
        /// </summary>
        /// <param name="config"></param>
        private void autoRadio50(GameConfig config)
        {
            if (stopAll)
            {
                MessageWrite(account, "已手动停止所有任务，退出50副本任务。");
                return;
            }
            if (!config.AutoRadio50)
            {
                MessageWrite(account, "配置文件设置为：不自动进行50副本，已跳过。");
                return;
            }

            GameState state = Manager.GetState(account);
            if (state.Radio50Date.ToShortDateString().Equals(DateTime.Now.ToShortDateString()))
            {
                if (state.Radio50Completed)
                {
                    MessageWrite(account, "今天的50副本已完成。");
                    return;
                }
            }

            if (config.RadioQuestType == RadioQuest.Radio50)
            {
                QuestacceptRadio50();//接受50困难副本任务
            }


            Forward(Operationer.RadioHard50EnterCode);//进入50困难
            Thread.Sleep(1000);

            int count = 1;
            int timeNeedWait = 5;
            bool radioComplete = false;
            OperationWaitResultPoint checkCompleteRadioPoint = new OperationWaitResultPoint();
            checkCompleteRadioPoint.Method = delegate(byte[] data, int length, OperationWaitResultPoint point)
            {
                radioComplete = FightRadio50Sucess(data, length);//检测是否通关
                point.Stoped = radioComplete;
                point.Sucess = radioComplete;
                return radioComplete;//如果已通关会从检测结果方法队列中删除
            };
            this.waitResultMethod.Add(checkCompleteRadioPoint);

            OperationWaitResultPoint getNextTimePoint = new OperationWaitResultPoint();
            int time = 0;
            getNextTimePoint.Method = delegate(byte[] data, int length, OperationWaitResultPoint point)
            {
                time = GetNextFightTime(data, length);
                return true;
            };
            this.waitResultMethod.Add(getNextTimePoint);
            state.Radio50Date = DateTime.Now;
            while (!radioComplete)
            {
                if (stopAll)
                {
                    MessageWrite(account, "已手动停止所有任务，退出50副本任务。");
                    getNextTimePoint.Stoped = true;
                    checkCompleteRadioPoint.Stoped = true;
                    return;
                }

                if (this.stopCurrent)
                {
                    MessageWrite(account, "已停止自动50副本.");
                    state.Radio50Completed = true;
                    this.stopCurrent = false;
                    checkCompleteRadioPoint.Stoped = true;
                    getNextTimePoint.Stoped = true;
                    return;
                }
                pauseEvent.WaitOne();//暂停
                MessageWrite(account, "进入50" + EnumConverter.GetString(config.Radio30Type) + "副本第" + count + "层战斗");
                FightRadio50(RadioType.Hard);

                WaitCommandSucess(getNextTimePoint, 10);//等待操作执行结果
                if (time > 0)
                {
                    MessageWrite(account, "还需要等待" + time + "秒才能进行下次战斗.");
                    Thread.Sleep((time + 3) * 1000);
                    if (time > 60)
                    {
                        if (timeNeedWait < 20)
                        {
                            timeNeedWait = 20;
                        }
                        else
                        {
                            timeNeedWait += 2;
                        }
                    }
                    else
                    {
                        timeNeedWait += 2;
                    }
                    continue;
                }

                Thread.Sleep(timeNeedWait * 1000);//战斗时间
                FinshFight();//结束战斗

                MessageWrite(account, "第" + (count++) + "层战斗结束");
                //WaitCommandSucess(point, 10);

            }
            checkCompleteRadioPoint.Stoped = true;
            getNextTimePoint.Stoped = true;

            state.Radio50Completed = true;
            if (config.RadioQuestType == RadioQuest.Radio50)
            {
                FinshQuestRadio50();
            }

            MessageWrite(account, "50副本已通关.");
        }
        #endregion

        #region 70困难副本

        /// <summary>
        /// 接受70困难副本任务
        /// </summary>
        /// <returns></returns>
        public bool QuestacceptRadio70()
        {
            MessageWrite(account, "接受70困难副本任务...");
            Forward(QuestacceptRadio70ACode);
            Thread.Sleep(1000);
            Forward(QuestacceptRadio70BCode);
            Thread.Sleep(1000);
            Forward(QuestacceptRadio70CCode);
            return true;
        }

        /// <summary>
        /// 提交70困难副本任务
        /// </summary>
        /// <returns></returns>
        public bool FinshQuestRadio70()
        {
            MessageWrite(account, "提交70困难副本任务...");
            Forward(FinshQuestRadio70ACode);
            Thread.Sleep(1000);
            Forward(FinshQuestRadio70BCode);
            Thread.Sleep(1000);
            Forward(FinshQuestRadio70CCode);
            return true;
        }

        /// <summary>
        /// 70副本进入下一层战斗
        /// </summary>
        /// <param name="radioType"></param>
        private void FightRadio70(RadioType radioType)
        {
            if (radioType == RadioType.Hard)
            {
                Forward(Operationer.RadioHard70NextFightCode);
            }
            else
            {
                Forward(Operationer.RadioEasy70NextFightCode);
            }
        }

        /// <summary>
        /// 70副本自动任务
        /// </summary>
        /// <param name="config"></param>
        private void autoRadio70(GameConfig config)
        {
            if (stopAll)
            {
                MessageWrite(account, "已手动停止所有任务，退出70副本任务。");
                return;
            }

            if (!config.AutoRadio70)
            {
                MessageWrite(account, "配置文件设置为：不自动进行70副本，已跳过。");
                return;
            }

            GameState state = Manager.GetState(account);
            if (state.Radio70Date.ToShortDateString().Equals(DateTime.Now.ToShortDateString()))
            {
                if (state.Radio70Completed)
                {
                    MessageWrite(account, "今天的70副本已完成。");
                    return;
                }
            }

            if (config.RadioQuestType == RadioQuest.Radio70)
            {
                QuestacceptRadio70();
            }

            MessageWrite(account, "进入困难副本");
            Forward(Operationer.RadioHard70EnterCode);//进入70困难
            Thread.Sleep(500);
            //UserItem(Item初级群体增强符, 1);
            //MessageWrite(account, "使用初级群体增强符");
            //Thread.Sleep(500);

            int count = 1;
            int timeNeedWait = 5;//战斗等待时间
            bool radioComplete = false;
            OperationWaitResultPoint checkCompleteRadioPoint = new OperationWaitResultPoint();
            checkCompleteRadioPoint.Method = delegate(byte[] data, int length, OperationWaitResultPoint point)
            {
                radioComplete = FightRadio70Sucess(data, length);//检测是否通关
                checkCompleteRadioPoint.Sucess = radioComplete;
                checkCompleteRadioPoint.Stoped = radioComplete;
                return radioComplete;//如果已通关会从检测结果方法队列中删除
            };
            this.waitResultMethod.Add(checkCompleteRadioPoint);

            int time = 0;
            OperationWaitResultPoint getNextTimePoint = new OperationWaitResultPoint();
            getNextTimePoint.Method = delegate(byte[] data, int length, OperationWaitResultPoint point)
            {
                time = GetNextFightTime(data, length);
                return true;
            };
            this.waitResultMethod.Add(getNextTimePoint);
            state.Radio70Date = DateTime.Now;
            while (!radioComplete)
            {
                if (stopAll)
                {
                    MessageWrite(account, "已手动停止所有任务，退出70副本任务。");
                    getNextTimePoint.Sucess = true;
                    getNextTimePoint.Stoped = true;
                    checkCompleteRadioPoint.Sucess = true;
                    checkCompleteRadioPoint.Stoped = true;
                    return;
                }

                if (this.stopCurrent)
                {
                    MessageWrite(account, "已终止自动70副本.");
                    state.Radio50Completed = true;
                    this.stopCurrent = false;
                    getNextTimePoint.Sucess = true;
                    getNextTimePoint.Stoped = true;
                    checkCompleteRadioPoint.Sucess = true;
                    checkCompleteRadioPoint.Stoped = true;
                    return;
                }
                pauseEvent.WaitOne();//暂停
                MessageWrite(account, "进入70" + EnumConverter.GetString(config.Radio30Type) + "副本第" + count + "次战斗");
                FightRadio70(RadioType.Hard);

                WaitCommandSucess(getNextTimePoint, 10);//等待操作执行结果
                if (time > 0)
                {
                    MessageWrite(account, "还需要等待" + time + "秒才能进行下次战斗.");
                    Thread.Sleep((time + 3) * 1000);
                    if (time > 60)
                    {
                        if (timeNeedWait < 20)
                        {
                            timeNeedWait = 20;
                        }
                        else
                        {
                            timeNeedWait += 2;
                        }
                    }
                    else
                    {
                        timeNeedWait += 2;
                    }
                    continue;
                }
                else
                {
                    if (timeNeedWait < 30)
                    {
                        timeNeedWait++;
                    }
                }
                Thread.Sleep(timeNeedWait * 1000);//战斗时间
                FinshFight();//结束战斗

                MessageWrite(account, "第" + (count++) + "次战斗结束");
                //WaitCommandSucess(point, 10);
            }

            getNextTimePoint.Sucess = true;
            getNextTimePoint.Stoped = true;
            checkCompleteRadioPoint.Sucess = true;
            checkCompleteRadioPoint.Stoped = true;
            state.Radio70Completed = true;
            if (config.RadioQuestType == RadioQuest.Radio70)
            {
                FinshQuestRadio70();
            }

            MessageWrite(account, "70副本已通关.");
        }


        #endregion

        #region 80困难副本


        /// <summary>
        /// 80副本进入下一层战斗
        /// </summary>
        /// <param name="radioType"></param>
        private void FightRadio80(RadioType radioType)
        {
            if (radioType == RadioType.Hard)
            {
                Forward(Operationer.RadioHard80NextFightCode);
            }
            else
            {
                Forward(Operationer.RadioEasy80NextFightCode);
            }
        }

        /// <summary>
        /// 80副本自动任务
        /// </summary>
        /// <param name="config"></param>
        private void autoRadio80(GameConfig config)
        {
            if (stopAll)
            {
                MessageWrite(account, "已手动停止所有任务，退出80副本任务。");
                return;
            }

            if (!config.AutoRadio80)
            {
                MessageWrite(account, "配置文件设置为：不自动进行80副本，已跳过。");
                return;
            }

            GameState state = Manager.GetState(account);
            if (state.Radio80Date.ToShortDateString().Equals(DateTime.Now.ToShortDateString()))
            {
                if (state.Radio80Completed)
                {
                    MessageWrite(account, "今天的80副本已完成。");
                    return;
                }
            }

            MessageWrite(account, "进入80困难副本");
            Forward(Operationer.RadioHard80EnterCode);//进入70困难
            Thread.Sleep(500);
            //UserItem(Item初级群体增强符, 1);
            //MessageWrite(account, "使用初级群体增强符");
            //Thread.Sleep(500);

            int count = 1;
            int timeNeedWait = 8;//战斗等待时间
            bool radioComplete = false;
            OperationWaitResultPoint checkCompleteRadioPoint = new OperationWaitResultPoint();
            checkCompleteRadioPoint.Method = delegate(byte[] data, int length, OperationWaitResultPoint point)
            {
                radioComplete = FightRadio80Sucess(data, length);//检测是否通关
                point.Sucess = radioComplete;
                point.Stoped = radioComplete;
                return radioComplete;//如果已通关会从检测结果方法队列中删除
            };
            this.waitResultMethod.Add(checkCompleteRadioPoint);

            OperationWaitResultPoint getNextTimePoint = new OperationWaitResultPoint();
            int time = 0;
            getNextTimePoint.Method = delegate(byte[] data, int length, OperationWaitResultPoint point)
            {
                time = GetNextFightTime(data, length);
                return true;
            };
            this.waitResultMethod.Add(getNextTimePoint);
            state.Radio80Date = DateTime.Now;
            while (!radioComplete)
            {
                if (stopAll)
                {
                    MessageWrite(account, "已手动停止所有任务，退出80副本任务。");
                    checkCompleteRadioPoint.Stoped = true;
                    getNextTimePoint.Stoped = true;
                    return;
                }

                if (this.stopCurrent)
                {
                    MessageWrite(account, "已终止80副本.");
                    checkCompleteRadioPoint.Stoped = true;
                    getNextTimePoint.Stoped = true;
                    state.Radio80Completed = true;
                    this.stopCurrent = false;
                    return;
                }
                pauseEvent.WaitOne();//暂停
                MessageWrite(account, "进入80" + EnumConverter.GetString(config.Radio80Type) + "副本第" + count + "次战斗");

                FightRadio80(RadioType.Hard);

                WaitCommandSucess(getNextTimePoint, 10);//等待操作执行结果
                if (time > 0)
                {
                    MessageWrite(account, "还需要等待" + time + "秒才能进行下次战斗.");
                    Thread.Sleep((time + 3) * 1000);
                    if (time > 60)
                    {
                        if (timeNeedWait < 20)
                        {
                            timeNeedWait = 20;
                        }
                        else
                        {
                            timeNeedWait += 2;
                        }
                    }
                    else
                    {
                        timeNeedWait += 2;
                    }
                    continue;
                }
                else
                {
                    if (timeNeedWait < 30)
                    {
                        timeNeedWait++;
                    }
                }
                Thread.Sleep(timeNeedWait * 1000);//战斗时间
                FinshFight();//结束战斗

                MessageWrite(account, "第" + (count++) + "次战斗结束");
                //WaitCommandSucess(point, 10);
            }

            state.Radio80Completed = true;
            checkCompleteRadioPoint.Stoped = true;
            getNextTimePoint.Stoped = true;
            MessageWrite(account, "80副本已通关.");
        }


        #endregion

        #region 老板娘日常任务
        /// <summary>
        /// 老板娘日常任务自动完成
        /// </summary>
        /// <param name="config"></param>
        private void autoDailyQuest(GameConfig config)
        {
            if (stopAll)
            {
                MessageWrite(account, "已手动停止所有任务，退出老板娘任务。");
                return;
            }

            if (!config.AutoDialyQuest)
            {
                MessageWrite(account, "配置文件设置为：不自动进行老板娘任务，已跳过。");
                return;
            }

            GameState state = Manager.GetState(account);
            if (state.DailyQuestDate.ToShortDateString().Equals(DateTime.Now.ToShortDateString()))
            {
                if (state.DailyQuestCompleted)
                {
                    MessageWrite(account, "今天的老板娘日常任务已完成。");
                    return;
                }
            }

            int count = 0;
            OperationWaitResultPoint getNextTimePoint = new OperationWaitResultPoint();
            int time = 0;
            getNextTimePoint.Method = delegate(byte[] data, int length, OperationWaitResultPoint point)
            {
                time = GetNextFightTime(data, length);
                return true;
            };
            this.waitResultMethod.Add(getNextTimePoint);

            state.DailyQuestDate = DateTime.Now;
            for (int i = 0; i < 4; i++)
            {
                #region 检测停止或暂停
                if (stopAll)
                {
                    MessageWrite(account, "已手动停止所有任务，退出老板娘任务。");
                    return;
                }
                if (this.stopCurrent)
                {
                    MessageWrite(account, "已停止自动老板娘日常任务.");
                    state.DailyQuestCompleted = true;
                    this.stopCurrent = false;
                    return;
                }
                pauseEvent.WaitOne();//暂停
                #endregion

                #region 完成任务:烽烟再起
            QuestacceptDaily1Code:
                count++;
                Forward(Operationer.QuestacceptDaily1Code, Operationer.QuestacceptDaily1Code.Length, serverClient);
                MessageWrite(account, "接受任务：烽烟再起...");

                byte[] datas = Tools.Contact(Operationer.TransmitCode, Operationer.Address黑风岗Code);
                MessageWrite(account, "移动到黑风岗");
                Forward(datas);
                Thread.Sleep(1000);
                datas = Tools.Contact(Operationer.AttackMonsterCode, Operationer.Monster黑风兽Code);
            攻击黑风兽:
                MessageWrite(account, "攻击黑风兽");
                Forward(datas);

                WaitCommandSucess(getNextTimePoint, 10);//等待操作执行结果
                if (time > 0)
                {
                    MessageWrite(account, "还需要等待" + time + "秒才能进行下次战斗.");
                    Thread.Sleep((time + 3) * 1000);
                    goto 攻击黑风兽;
                }

                Thread.Sleep(5000);
                Forward(Operationer.FightCompleteCode);
                MessageWrite(account, "战斗结束");
                Thread.Sleep(100);
                Forward(Operationer.FinishQuestDaily1Code);
                MessageWrite(account, "提交任务：烽烟再起");
                #endregion

                #region 检测停止或暂停
                if (stopAll)
                {
                    MessageWrite(account, "已手动停止所有任务，退出老板娘任务。");
                    return;
                }
                if (this.stopCurrent)
                {
                    MessageWrite(account, "已停止自动老板娘日常任务.");
                    state.DailyQuestCompleted = true;
                    this.stopCurrent = false;
                    return;
                }
                pauseEvent.WaitOne();//暂停
                #endregion

                #region 完成任务：老板娘叫你回家吃饭
                count = 0;
            QuestacceptDaily2Code:
                count++;
                Forward(Operationer.QuestacceptDaily2Code);
                MessageWrite(account, "接受任务：老板娘叫你回家吃饭");

                datas = Tools.Contact(Operationer.TransmitCode, Operationer.Address黑森林Code);
                MessageWrite(account, "移动到黑森林");
                Forward(datas);
                Thread.Sleep(1000);
                datas = Tools.Contact(Operationer.AttackMonsterCode, Operationer.Monster僵尸刺客Code);
                MessageWrite(account, "攻击僵尸刺客");
                Forward(datas);
                Thread.Sleep(5000);
                Forward(Operationer.FightCompleteCode);
                MessageWrite(account, "战斗结束");
                Thread.Sleep(1000);
                Forward(Operationer.FinishQuestDaily2Code);
                MessageWrite(account, "提交任务：老板娘叫你回家吃饭");

                #endregion

                #region 检测停止或暂停
                if (stopAll)
                {
                    MessageWrite(account, "已手动停止所有任务，退出老板娘任务。");
                    return;
                }
                if (this.stopCurrent)
                {
                    MessageWrite(account, "已停止自动老板娘日常任务.");
                    state.DailyQuestCompleted = true;
                    this.stopCurrent = false;
                    return;
                }
                pauseEvent.WaitOne();//暂停
                #endregion

                #region 完成任务：有人的地方就有江湖
                count = 0;
            QuestacceptDaily3Code:
                count++;
                Forward(Operationer.QuestacceptDaily3Code);
                MessageWrite(account, "接受任务：有人的地方就有江湖");

                datas = Tools.Contact(Operationer.TransmitCode, Operationer.Address战争废墟Code);
                MessageWrite(account, "移动到战争废墟");
                Forward(datas);
                Thread.Sleep(1000);
                datas = Tools.Contact(Operationer.AttackMonsterCode, Operationer.Monster怨灵鬼魂Code);
                MessageWrite(account, "攻击怨灵鬼魂");
                Forward(datas);
                Thread.Sleep(5000);
                Forward(Operationer.FightCompleteCode);
                MessageWrite(account, "战斗结束");
                Thread.Sleep(1000);
                Forward(Operationer.FinishQuestDaily3Code);
                MessageWrite(account, "提交任务：有人的地方就有江湖");
                #endregion

                #region 检测停止或暂停
                if (stopAll)
                {
                    MessageWrite(account, "已手动停止所有任务，退出老板娘任务。");
                    return;
                }
                if (this.stopCurrent)
                {
                    MessageWrite(account, "已停止自动老板娘日常任务.");
                    state.DailyQuestCompleted = true;
                    this.stopCurrent = false;
                    return;
                }
                pauseEvent.WaitOne();//暂停
                #endregion

                #region 完成任务：人争一口气
                count = 0;
            QuestacceptDaily4Code:
                count++;
                Forward(Operationer.QuestacceptDaily4Code);
                MessageWrite(account, "接受任务：人争一口气");
                datas = Tools.Contact(Operationer.TransmitCode, Operationer.Address码头废墟Code);
                MessageWrite(account, "移动到码头废墟");
                Forward(datas);
                Thread.Sleep(1000);
                datas = Tools.Contact(Operationer.AttackMonsterCode, Operationer.Monster渔人头目Code);
                MessageWrite(account, "攻击渔人头目");
                Forward(datas);
                Thread.Sleep(6000);
                Forward(Operationer.FightCompleteCode);
                MessageWrite(account, "战斗结束");
                Thread.Sleep(1000);
                Forward(Operationer.FinishQuestDaily4Code);
                MessageWrite(account, "提交任务：人争一口气");
                #endregion
            }
            state.DailyQuestCompleted = true;
            getNextTimePoint.Stoped = true;
        }
        #endregion

        #region 循环经验任务

        private void fluashExpTaskClolor(GameConfig config)
        {
            //注册获取经验值的监听器
            ExpTaskColor color;
            int nextFluashTime = 0;

            #region 刷新循环任务监听器
            OperationWaitResultPoint fluashExpTaskColor = new OperationWaitResultPoint(60);
            fluashExpTaskColor.Enabled = false;
            fluashExpTaskColor.Method = new OperationWaitResult(delegate(byte[] datas, int length, OperationWaitResultPoint listener)
            {
                int index = Tools.SearchIndex(datas, length, Operationer.FlushQuestColorReturn);
                if (index > -1)
                {
                    listener.Sucess = true;
                    listener.Stoped = true;

                    color = (ExpTaskColor)datas[index + Operationer.FlushQuestColorReturn.Length];
                    nextFluashTime = BitConverter.ToInt16(datas, index + Operationer.FlushQuestColorReturn.Length + 1);
                    MessageWrite(account, "刷新循环任务颜色成功，现在的颜色是：" + color.ToString() + "，离下次刷新还有：" + new TimeSpan(0, 0, nextFluashTime).ToString());
                    if (config.NeedExpTaskColor == color)
                    {
                        MessageWrite(account, "已达到配置文件设置的最低循环任务颜色，开始进行循环经验任务。");
                        autoExpQuest(config);
                    }
                    else
                    {
                        MessageWrite(account, "没有达到配置文件设置的最低循环任务颜色，等待下次刷新。");
                        //注册计时器
                    }
                    return true;
                }
                return false;
            });
            fluashExpTaskColor.OnTimeout += new OperationWaitResultTimeout(delegate(int timeout)
            {
                MessageWrite(account, "刷新循环经验任务颜色的监听器已经超时，任务可能没有刷新成功...");
            });
            waitResultMethod.Add(fluashExpTaskColor);
            #endregion

            #region 查询循环任务信息监听器
            OperationWaitResultPoint queryExpTaskColor = new OperationWaitResultPoint();
            queryExpTaskColor.Method = new OperationWaitResult(delegate(byte[] datas, int length, OperationWaitResultPoint listener)
            {
                int index = Tools.SearchIndex(datas, length, Operationer.QueryQuestInfoResult);
                if (index > -1)
                {
                    color = (ExpTaskColor)datas[index + Operationer.QueryQuestInfoResult.Length];//颜色
                    nextFluashTime = BitConverter.ToInt16(datas, index + Operationer.QueryQuestInfoResult.Length + 7);
                    int sequence = (int)datas[index + 15]+1;//当前任务的次数
                    string taskName = GetExpTaskName(datas, length);
                    currentExpTask = new ExpTask(TaskDatabase.GetTask(taskName),color,sequence);
                    if (currentExpTask != null)
                    {
                        listener.Sucess = true;
                        listener.Stoped = true;
                        MessageWrite(account, "获取到循环经验任务信息：" + color.ToString() + " " + currentExpTask.Name);

                        if (nextFluashTime == 0)
                        {
                            MessageWrite(account, "当前可以免费刷新.");
                            fluashExpTaskColor.Enabled = true;
                            Forward(Operationer.FlushQuestColor);//刷新循环任务
                            MessageWrite(account, "免费刷新循环经验任务.");
                        }
                        else
                        {
                            TimeSpan timeSpan = new TimeSpan(0, 0, nextFluashTime);
                            MessageWrite(account, "距离下次免费刷新还需要：" + timeSpan.ToString());
                        }
                    }
                    return true;
                }
                return false;
            });
            queryExpTaskColor.OnTimeout += new OperationWaitResultTimeout(delegate(int timeout)
            {
                MessageWrite(account, "查询循环经验任务颜色的监听器已经超时，无法获取循环任务的信息...");
            });
            #endregion



            waitResultMethod.Add(queryExpTaskColor);

            MessageWrite(account, "获取循环经验任务的颜色...");
            Transmit(MapDatabase.GetAddress("山河郡.山河城"));
            Thread.Sleep(100);
            Forward(Operationer.QueryQuestInfo);
            //fluashExpTaskColor.Wait();
            queryExpTaskColor.Wait();
        }

        /// <summary>
        /// 循环经验任务自动完成
        /// </summary>
        /// <param name="config"></param>
        private void autoExpQuest(GameConfig config)
        {
            if (stopAll)
            {
                MessageWrite(account, "已手动停止所有任务，退出循环经验任务。");
                return;
            }

            if (!config.AutoExpQuest)
            {
                MessageWrite(account, "配置文件设置为：不自动进行循环经验，已跳过。");
                return;
            }

            GameState state = Manager.GetState(account);
            if (state.ExpQuestDate.ToShortDateString().Equals(DateTime.Now.ToShortDateString()))
            {
                if (state.ExpQuestCompleted)
                {
                    MessageWrite(account, "今天的循环经验任务已完成。");

                    return;
                }
            }

            if (config.NeedExpTaskColor == currentExpTask.Color)
            {
                MessageWrite(account, "已达到配置文件设置的最低循环经验任务颜色，开始进行循环经验任务。");
                //autoExpQuest(config);
            }
            else
            {
                MessageWrite(account, "没有达到配置文件设置的最低循环经验任务颜色，等待下次刷新。");
                return;
                //注册计时器
            }

            int count = 0;
            OperationWaitResultPoint getNextTaskLisenter = new OperationWaitResultPoint(60);
            getNextTaskLisenter.Method = new OperationWaitResult(delegate(byte[] datas, int length, OperationWaitResultPoint listener)
            {
                int index = Tools.SearchIndex(datas, length, Operationer.QueryQuestInfoResult);
                if (index > -1)
                {
                    ExpTaskColor color = (ExpTaskColor)datas[index + Operationer.QueryQuestInfoResult.Length];
                    int taskCount = (int)datas[index + 15] + 1;//当前的次数

                    string taskName = GetExpTaskName(datas, length);
                    currentExpTask = new ExpTask(TaskDatabase.GetTask(taskName), color, taskCount);
                    currentExpTask.Completed=datas[index + 25]==1?false:true;
                    if (currentExpTask != null)
                    {
                        listener.Sucess = true;
                        listener.Enabled = false;
                        MessageWrite(account, "开始循环任务：" + color.ToString() + " " + currentExpTask.Name + "(第" + taskCount + "次。)");
                    }
                }
                return true;
            });
            waitResultMethod.Add(getNextTaskLisenter);
            state.ExpQuestDate = DateTime.Now;
            while (!state.ExpQuestCompleted)
            {
                #region 检测停止或暂停
                if (stopAll)
                {
                    MessageWrite(account, "已手动停止所有任务，退出循环经验任务。");
                    return;
                }
                if (this.stopCurrent)
                {
                    MessageWrite(account, "已停止自动循环经验任务.");
                    state.ExpQuestCompleted = true;
                    this.stopCurrent = false;
                    return;
                }
                pauseEvent.WaitOne();//暂停
                #endregion

                getNextTaskLisenter.Enabled = true;
                Forward(Operationer.QueryQuestInfo);//获取循环任务信息

                if (getNextTaskLisenter.Wait())
                {
                    if (!currentExpTask.Completed)
                    {
                        TakeTask(currentExpTask);
                        Transmit(MapDatabase.GetAddress(currentExpTask.AddressName));
                        AttackMonster(MonsterDatabase.GetMonster(currentExpTask.MonsterName), 5);
                    }
                    if (CompleteTask(currentExpTask))
                    {
                        MessageWrite(account, "提交任务失败.");
                    }
                    count = 0;
                }
                else
                {
                    if (count < 3)
                    {
                        MessageWrite(account, "获取任务信息失败，已超时(60秒)，重试第" + (count++) + "次.");
                    }
                    else
                    {
                        MessageWrite(account, "获取任务信息失败，已超时(60秒)，连续重试了" + count + "次，不再进行循环任务.");
                        return;
                    }
                }

                Thread.Sleep(100);

            }

            state.ExpQuestCompleted = true;

        }
        #endregion

        #region 自动打怪
        /*
        private void AutoAttackMonster(GameConfig config)
        {
            int count = 3;
            bool transmitSucess = false;
            for (int i = 0; i < count && !transmitSucess; i++)
            {
                transmitSucess = Transmit(Address海波流);
            }

            if (!transmitSucess)
            {
                MessageWrite(account, "命令：传送到" + Address海波流.Name + " 失败3次，自动打怪已停止.");
            }

            int nextFightTime = 1;//战斗时间

            while (serverClient.Connected && !stopCurrent)
            {

            }
        }*/
        #endregion



        #region 获取游戏信息
        /// <summary>
        /// 获取角色等级信息
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        private bool GetCharactersLevelInfo(byte[] datas, int length, OperationWaitResultPoint listener)
        {
            //查找角色数据开头
            int index = Tools.SearchIndex(datas, length, Operationer.SysCharactersInfoStart);
            if (index > -1)
            {
                listener.Sucess = true;
                listener.Stoped = true;
                account.Character.LV = (int)datas[index + 14];
                MessageWrite(account, "获得角色信息->等级:" + account.Character.LV);
                if (thread == null)
                {
                    if (this.OnConnected != null)
                    {
                        OnConnected(account);
                    }
                    BeginQuest();//找到角色等级后开始自动任务，应为钟宝宝账户找不到等级信息，换成获取到礼券等信息后就开始任务
                }
                return true;
            }
            return false;
        }

        private void getCharactersLevel_OnTimeout(int timeout)
        {
            MessageWrite(account, "获得角色等级已超时.");
            if (thread == null)
            {
                if (this.OnConnected != null)
                {
                    OnConnected(account);
                }
                BeginQuest();//找到角色等级后开始自动任务，因为钟宝宝账户找不到等级信息，换成超时后也开始任务
            }
        }

        /// <summary>
        /// 获取角色其他信息
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        private bool GetCharactersExtInfo(byte[] datas, int length, OperationWaitResultPoint listener)
        {
            //查找角色数据开头
            int index = Tools.SearchIndex(datas, length, Operationer.SysCharactersInfoStartA);
            if (index > -1)
            {
                listener.Sucess = true;
                listener.Stoped = true;

                index += Operationer.SysCharactersInfoStartA.Length;
                account.Character.GodCoin = (BitConverter.ToInt32(datas, index));
                MessageWrite(account, "获得角色信息->元宝：" + account.Character.GodCoin);
                int GiftCertificate = BitConverter.ToInt32(datas, index + 4);
                account.Character.GiftCertificate = (GiftCertificate);
                MessageWrite(account, "获得角色信息->礼券：" + account.Character.GiftCertificate);
                account.Character.SliveCoin = (BitConverter.ToInt32(datas, index + 8));
                MessageWrite(account, "获得角色信息->银币：" + account.Character.SliveCoin);
                account.Character.CopperCoin = (BitConverter.ToInt32(datas, index + 12));
                MessageWrite(account, "获得角色信息->铜币：" + account.Character.CopperCoin);

                if (thread == null)
                {
                    if (this.OnConnected != null)
                    {
                        OnConnected(account);
                    }

                    BeginQuest();//找到角色等级后开始自动任务，应为钟宝宝账户找不到等级信息，换成获取到礼券等信息后就开始任务
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取经验值信息
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        private bool GetExp(byte[] datas, int length, OperationWaitResultPoint listener)
        {
            int exp = Tools.GetExp(datas, length);
            if (exp != -1)
            {
                MessageWrite(account, "获的经验值" + exp);
                listener.Sucess = true;
                listener.Enabled = false;
            }
            return false;
        }

        private void getCharactersExtInfo_OnTimeout(int timeout)
        {
            MessageWrite(account, "获得角色信息已超时.");
            if (thread == null)
            {
                if (this.OnConnected != null)
                {
                    OnConnected(account);
                }
                BeginQuest();//找到角色等级后开始自动任务，因为钟宝宝账户找不到等级信息，换成超时后也开始任务
            }
        }

        #endregion

        #region 提示信息输出
        private void SysWriteMessage(string message)
        {
            MessageWrite(null, DateTime.Now.ToString("hh:mm:ss") + " " + account.ToString("_") + message);
        }
        #endregion
        #endregion
    }
}
